﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NhanAi.Entity;
using NhanAi.DAO;
using NhanAi.DAO.Mix;
using System.Threading;
using System.Globalization;
using System.Resources;
namespace NhanAi.Public
{
    public partial class PostView : PublicBasePage
    {
        Post post;
        int cateId;
        List<Post> relatedPosts;
        public int CateId { get { return cateId; } }
        string message;
        public Post Post { get { return post; } }
        public List<Post> RelatedPosts { get { return relatedPosts; } }
        public string Message { get { return message; } }
        //protected override void InitializeCulture()
        //{
        //    base.InitializeCulture();
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            PostModel modPost = new PostModel();
            CommentDao daoCom = new CommentDao();
            int id = Home.Utils.TypeWrapper.GetInt32(Request["id"]);
            int cate = Home.Utils.TypeWrapper.GetInt32(Request["cate"]);
            Post p = modPost.Read(id, CurrentLanguageCode);
            this.post = p;
            cateId = cate;
            if (p != null)
            {
                int total = 0;
                List<Comment> cms = daoCom.ReadMany(25, 1, p.Id, null, null, true, null, null, out total);
                p.Comments = cms;
                modPost.UpdateViewCount(p.Id, 1);
            }
            if (cate > 0)
            {
                PostDao dao = new PostDao();
                int s_total = 0;
                relatedPosts = dao.ReadByTaxonomy(7, 1, CurrentLanguageCode, "post", cate, null, false, -1, NhanAi.App_Code.NAConstants.POST_STA_POST, null, null, out s_total);
                if (p != null) foreach (NhanAi.Entity.Post tmp in relatedPosts)
                    {
                        if (tmp.Id == p.Id) { relatedPosts.Remove(tmp); break; }
                    }
            }
            if (Request.HttpMethod == "POST" && !String.IsNullOrEmpty(Request["submit-comment"]))
            {
                string email = Request["email"] == null ? "" : Request["email"];
                string author = Request["author"] == null ? "" : Request["author"];
                string content = Request["comment"] == null ? "" : Request["comment"];
              
                if (email == "" || author == "" || content == "")
                {
                    message = "Vui lòng nhập các thông tin yêu cầu";
                }
                else
                {
                   
                    Comment c = new Comment();
                    if (post != null) c.PostId = p.Id;
                    c.Author = author;
                    c.AuthorEmail = email;
                    c.CommentContent = content;
                    c.CommentDate = DateTime.Now;
                    c.Approved = false;
                    c.CommentType = "post";
                    
                    try
                    {
                        daoCom.Create(c);
                        modPost.UpdateCommentCount(p.Id, 1);                        
                        message = GetGlobalResourceObject("HmLanguage", "comment_success").ToString();
                    }
                    catch (Exception ex)
                    {
                        message = GetGlobalResourceObject( "HmLanguage","comment_fail").ToString();
                    }
                }
            }
        }

       
    }
}